package com.salton123.utils

import android.content.Context
import android.graphics.Bitmap
import android.widget.ImageView
import androidx.annotation.DrawableRes
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.transition.Transition
import com.hyphenate.easeim.BuildConfig
import com.hyphenate.easeim.R
import com.salton123.log.XLog

/**
 * User: newSalton@outlook.com
 * Date: 2019-08-04 12:46
 * ModifyTime: 12:46
 * Description:
 */
object ImageLoaders {
    fun loadCircleCrop(
        imageView: ImageView, url: String,
        @DrawableRes holderResId: Int = R.drawable.ic_default_avatar
    ) {
        if (BuildConfig.APP_DEVELOP) {
            XLog.d(ImageLoaders::class.java, "[loadCircleCrop] url:$url")
        }
        Glide.with(imageView)
            .load(url)
            .centerCrop()
            .placeholder(holderResId)
            .circleCrop()
            .into(imageView)
    }

    fun load(imageView: ImageView, url: String, @DrawableRes holderResId: Int) {
        if (BuildConfig.APP_DEVELOP) {
            XLog.d(ImageLoaders::class.java, "[loadCircleCrop] url:$url")
        }
        Glide.with(imageView)
            .load(url)
            .placeholder(holderResId)
            .into(imageView)
    }

    @JvmOverloads
    fun loadFitCenter(imageView: ImageView, url: String, @DrawableRes holderResId: Int) {
        if (BuildConfig.APP_DEVELOP) {
            XLog.d(ImageLoaders::class.java, "[loadCenterCrop],url:$url")
        }
        Glide.with(imageView)
            .load(url)
            .centerCrop()
            .into(imageView)
    }

    @JvmOverloads
    fun loadCrop(imageView: ImageView, url: String, @DrawableRes holderResId: Int) {
        if (BuildConfig.APP_DEVELOP) {
            XLog.d(ImageLoaders::class.java, "[loadCenterCrop],url:$url")
        }
        Glide.with(imageView)
            .load(url)
            .centerInside()
            .into(imageView)
    }

    fun loadBitmap(context: Context?, url: String?, onLoadBitmapCallback: OnLoadBitmapCallback?) {
        Glide.with(context!!)
            .asBitmap().load(url)
            .centerInside()
            .into(object : SimpleTarget<Bitmap?>() {
                override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap?>?) {
                    onLoadBitmapCallback?.onBitmapCallBack(resource)
                }
            })
    }

    fun loadCenterInsideRoundedCorners(
        imageView: ImageView?,
        url: String,
        sizeMultiplier: Float,
        roundingRadius: Int
    ) {
        XLog.d(
            ImageLoaders::class.java,
            "[loadCenterInside] sizeMultiplier:" + sizeMultiplier
                + ",roundingRadius:" + roundingRadius + ",url:" + url)
        Glide.with(imageView!!)
            .load(url)
            .placeholder(R.drawable.image_loading)
            .centerInside()
            .into(imageView)
    }

    interface OnLoadBitmapCallback {
        fun onBitmapCallBack(bitmap: Bitmap?)
    }
}